 



import UIKit
import iOSDFULibrary
import CoreBluetooth

enum PresentationType {
    case push, present
}

protocol DFURouterType:class {
    @discardableResult//取消警告关键字,当有返回值的函数,返回值没有被接收时会出现!警告
    func initialState() -> UIViewController
    
    func getStartViewController() -> DFUStartViewController
    //搜索连接蓝牙 ,连接完成后调用 闭包 -->再选择文件
    @discardableResult func goToBluetoothConnector(scanner: PeripheralScanner, presentationType: PresentationType, callback: @escaping (Peripheral) -> () ) -> ConnectionViewController
    //选择文件
    @discardableResult func goToFileSelection() -> DFUFileSelectorViewController
    //升级包信息
    @discardableResult func goToFirmwareInfo(firmware: DFUFirmware) -> DFUFirmwareInfoViewController
    //升级
    func goToUpdate(firmware: DFUFirmware, peripheral: Peripheral)
    
}



open class DFURouter:NSObject,DFURouterType {
    
    //点击更新
    func goToUpdate(firmware: DFUFirmware, peripheral: Peripheral) {
        let vc = DFUUpdateTabBarViewController(router: self, firmware: firmware, peripheral: peripheral)
        //navigationController.setViewControllers([vc], animated: true)
        navigationController.pushViewController(vc, animated: false)
    }
    
    private let btManager = PeripheralHolder()
    
    let navigationController: UINavigationController
    
    private var storedBluetoothCallback: ((Peripheral) -> ())!
    
   @objc init(navigationController: UINavigationController) {
        
        self.navigationController = navigationController
        super.init()
        btManager.callback = self
    }
    //升级文件信息
   @objc  func goToFirmwareInfo(firmware: DFUFirmware) -> DFUFirmwareInfoViewController {
        let vc = DFUFirmwareInfoViewController(firmware: firmware, bluetoothManager: btManager, router: self)
        navigationController.pushViewController(vc, animated: false)
        return vc
    }
    //选择文件
    @discardableResult
    func goToFileSelection() -> DFUFileSelectorViewController {
        let vc = DFUFileSelectorViewController(router: self, documentPicker: DFUDocumentPicker())
        navigationController.pushViewController(vc, animated: false)
        return vc
    }
    //新增方法,在升级之前先把文件选择好,后续都采用这个文件来升级
    @objc func openSelectFileVC(){
      let vc =  goToFileSelection()
        vc.isSelectFile = true
    }
    //连接蓝牙
     @discardableResult
      func goToBluetoothConnector(scanner: PeripheralScanner, presentationType: PresentationType = .push, callback: @escaping (Peripheral) -> () ) -> ConnectionViewController {
        //连接后的回调
        storedBluetoothCallback = callback
        
        let vc = ConnectionViewController(scanner: scanner, presentationType: presentationType)
        vc.delegate = btManager
        
        switch presentationType {
        case .present:
            let nc = UINavigationController.nordicBranded(rootViewController: vc, prefersLargeTitles: false)
            navigationController.present(nc, animated: true)
        case .push:
            navigationController.pushViewController(vc, animated: false)
        }
        
        return vc
    }
    
  @objc  func getStartViewController() -> DFUStartViewController {
        DFUStartViewController(router: self)
    }
    
  @objc func initialState() -> UIViewController {
        let vc = getStartViewController()
        navigationController.viewControllers = [vc]
        return navigationController
    }
    
}

extension DFURouter: PeripheralConnectionCallback {
    func peripheralWasSelected(_ peripheral: Peripheral) {
        //去选择文件
        storedBluetoothCallback(peripheral)
    }
}
